package com.ruoyi.web.controller.jjm;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.jjm.domain.Supplier;
import com.ruoyi.jjm.service.IClientUserOrderService;
import com.ruoyi.jjm.service.ISupplierService;
import com.ruoyi.jjm.utils.SupplierOrderStatus;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.service.ISysDeptService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.jjm.domain.SupplierOrder;
import com.ruoyi.jjm.service.ISupplierOrderService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 供应商订单Controller
 * 
 * @author tsr
 * @date 2020-07-15
 */
@Controller
@RequestMapping("/jjm/order")
public class SupplierOrderController extends BaseController
{
    private String prefix = "jjm/order";

    public static Map<Integer,String> orderStatusMap  = new HashMap<>();

    static {
        orderStatusMap.put(0,"未付款");
        orderStatusMap.put(1,"未收到煤");
        orderStatusMap.put(2,"未收到煤");
        orderStatusMap.put(3,"未收到煤");
        orderStatusMap.put(4,"已收到煤");
    }
    public static Map<Integer,String> supplierOrderStatusMap  = new HashMap<>();

    static {
        supplierOrderStatusMap.put(0,"未付款");
        supplierOrderStatusMap.put(1,"已付款");
        supplierOrderStatusMap.put(2,"未发货");
        supplierOrderStatusMap.put(3,"已发货");
        supplierOrderStatusMap.put(4,"已收货");
    }

    @Autowired
    private ISupplierService supplierService;
    @Autowired
    private ISupplierOrderService supplierOrderService;
    @Autowired
    private IClientUserOrderService clientUserOrderService;

    @Autowired
    private ISysDeptService sysDeptService;

//    @RequiresPermissions("jjm:order:view")
    @GetMapping()
    public String order(Long villageId,ModelMap mmap)
    {
        mmap.put("villageId",villageId);
        return prefix + "/order";
    }

    @GetMapping("/supplierOrder")
    public String supplierOrder(ModelMap mmap)
    {
        SysUser sysUser = ShiroUtils.getSysUser();
        Supplier supplier = supplierService.selectSupplierByUserId(ShiroUtils.getUserId());
        if(supplier == null){
            supplier = new Supplier();
            supplier.setId(0L);
            supplier.setUserId(sysUser.getUserId());
        }
        mmap.put("user",sysUser);
        mmap.put("supplier",supplier);
        if(supplier != null){
            Double totalAmount = supplierOrderService.sumTotalAmount(supplier.getId(),supplier.getAreaId());
            Double notDeliverAmount = supplierOrderService.sumNotDeliverAmount(supplier.getId(),supplier.getAreaId());
            mmap.put("totalAmount",totalAmount == null ? 0 : totalAmount);
            mmap.put("notDeliverAmount",notDeliverAmount == null ? 0 : notDeliverAmount);
        }
        return prefix + "/supplierOrder";
    }

    @GetMapping("/confirmList")
    public String confirmList(Long villageId,ModelMap mmap)
    {
        SysUser sysUser = ShiroUtils.getSysUser();
        Double sum = supplierOrderService.sumConfirmByVillage(sysUser.getDeptId());
        mmap.put("user",sysUser);
        mmap.put("sum",sum);
        return prefix + "/confirm";
    }
    /**
     * 查询供应商订单列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SupplierOrder supplierOrder)
    {
        startPage();
        List<SupplierOrder> list = supplierOrderService.selectSupplierOrderList(supplierOrder);
        if(CollectionUtils.isNotEmpty(list)){
            for(SupplierOrder order : list){
                if(order.getStatus() != null){
                    String statusValue = orderStatusMap.get(order.getStatus());
//                    if(order.getStatus().intValue() == SupplierOrderStatus.HAS_RECEIVED.ordinal() && order.getConfirmTime() != null){
//                        statusValue = DateUtils.parseDateToStr("yyyy-MM-dd",order.getConfirmTime())+statusValue;
//                    }
                    order.setStatusValue(statusValue);
                }
            }
        }
        return getDataTable(list);
    }
    @PostMapping("/listBySupplier")
    @ResponseBody
    public TableDataInfo listBySupplier(SupplierOrder supplierOrder)
    {
        startPage();
        List<SupplierOrder> list = supplierOrderService.selectSupplierOrderList(supplierOrder);
        if(CollectionUtils.isNotEmpty(list)){
            for(SupplierOrder order : list){
                if(order.getStatus() != null){
                    String statusValue = supplierOrderStatusMap.get(order.getStatus());
                    order.setStatusValue(statusValue);
                }
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出供应商订单列表
     */
    @RequiresPermissions("jjm:order:export")
    @Log(title = "供应商订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SupplierOrder supplierOrder)
    {
        List<SupplierOrder> list = supplierOrderService.selectSupplierOrderList(supplierOrder);
        ExcelUtil<SupplierOrder> util = new ExcelUtil<SupplierOrder>(SupplierOrder.class);
        return util.exportExcel(list, "order");
    }

    /**
     * 新增供应商订单
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存供应商订单
     */
//    @RequiresPermissions("jjm:order:add")
    @Log(title = "供应商订单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    @Transactional
    public AjaxResult addSave(SupplierOrder supplierOrder)
    {
        SysUser user = ShiroUtils.getSysUser();
        String address = sysDeptService.mergeAddress(user.getDept());
        supplierOrder.setAddress(address);
        supplierOrder.setPayTime(new Date());
        supplierOrder.setVillageId(user.getDeptId());
        supplierOrder.setStatus(SupplierOrderStatus.HAS_PAY.ordinal());
        supplierOrder.setCreateBy(user.getLoginName());
        supplierOrder.setUpdateBy(user.getLoginName());
        supplierOrderService.insertSupplierOrder(supplierOrder);
        clientUserOrderService.updateSupplierOrderIdByStatus(supplierOrder.getSupplierId(),1,user.getDeptId());
        return toAjax(1);
    }

    /**
     * 修改供应商订单
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        SupplierOrder supplierOrder = supplierOrderService.selectSupplierOrderById(id);
        mmap.put("supplierOrder", supplierOrder);
        return prefix + "/edit";
    }

    /**
     * 修改保存供应商订单
     */
    @RequiresPermissions("jjm:order:edit")
    @Log(title = "供应商订单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SupplierOrder supplierOrder)
    {
        return toAjax(supplierOrderService.updateSupplierOrder(supplierOrder));
    }

    /**
     * 删除供应商订单
     */
    @RequiresPermissions("jjm:order:remove")
    @Log(title = "供应商订单", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(supplierOrderService.deleteSupplierOrderByIds(ids));
    }

    /**
     * 确认收煤
     * @param id
     * @return
     */
    @Log(title = "确认收煤", businessType = BusinessType.UPDATE)
    @PostMapping( "/confirm/{id}")
    @ResponseBody
    public AjaxResult confirm(@PathVariable("id") Long id)
    {
        SysUser user = ShiroUtils.getSysUser();
        SupplierOrder order = new SupplierOrder();
        order.setId(id);
        order.setStatus(SupplierOrderStatus.HAS_RECEIVED.ordinal());
        order.setUpdateBy(user.getLoginName());
        order.setConfirmTime(new Date());
        return toAjax(supplierOrderService.updateSupplierOrder(order));
    }

    /**
     * 发货
     * @param id
     * @return
     */
    @Log(title = "发货", businessType = BusinessType.UPDATE)
    @PostMapping( "/deliver/{id}")
    @ResponseBody
    public AjaxResult deliver(@PathVariable("id") Long id)
    {
        SysUser user = ShiroUtils.getSysUser();
        SupplierOrder order = new SupplierOrder();
        order.setId(id);
        order.setStatus(SupplierOrderStatus.HAS_DELIVER.ordinal());
        order.setUpdateBy(user.getLoginName());
        order.setDeliverTime(new Date());
        return toAjax(supplierOrderService.updateSupplierOrder(order));
    }
}
